<%@page import="java.net.URLEncoder"%>
<%@page import="com.spmanage.util.HttpUtil"%>
<%@page import="com.spmanage.util.DateUtil"%>
<%@page import="com.spmanage.util.BillingUtil"%><%@page import="com.spmanage.util.DbOperation"%>
<%@page import="com.spmanage.util.SqlUtil"%><%@page import="com.spmanage.util.WebAction"%>
<%@page import="com.spmanage.util.StringUtil"%><%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%><%
    	WebAction webAction = new WebAction(request,response);
    
   
    String dtype = webAction.getParameterString("dtype");         

	if(dtype!=null){
		response.getWriter().write("0");
		if(dtype.equals("mo")){
		    System.out.println("a8接口-上行同步");
			String stype = webAction.getParameterString("stype");       
			String atype = webAction.getParameterString("atype");           
			String gwid = webAction.getParameterString("gwid");                
			String src = webAction.getParameterString("src");               
			String spNo  = webAction.getParameterString("spnumber");            
			String userPhone = webAction.getParameterString("usernumber");       
			String linkId = webAction.getParameterString("linkid");           
			String srvcode = webAction.getParameterString("srvcode");        
			String fee = webAction.getParameterString("fee");             
			String mid = webAction.getParameterString("mid");         
			//String date = webAction.getParameterString("date");         
			String mo = webAction.getParameterString("msg");    
			if(spNo==null||userPhone==null||linkId==null||mo==null){
				 response.getWriter().write("1");
			}else{
				 response.getWriter().write("0");
			    String date = DateUtil.nowTimeTransformCharacter(DateUtil.getNow_Timestamp().toString());
			    
				//指令处理,返回处理后的指令
				String strMo = BillingUtil.getMo(spNo, mo);
				System.out.println("a8接口-上行同步：原始上行:"+mo+";处理后的上行："+strMo);
			    //获取用户渠道号
			    int channelId = BillingUtil.getUserChannel(spNo,strMo);
			    //sp标识，类似于网关号
			    int spid = BillingUtil.getUserSpId(spNo,strMo);
				//省份
				int provinceId = BillingUtil.getProvinceId(userPhone);
			   
				//---------------扣量处理
				//得到CP渠道扣量比例数
			    int cpKouLiangShu = BillingUtil.getCpKouLiangBiLv(channelId);
				int cpKouLiang = 0;
				int ischeck = 0;
			    //得到CP渠道指令扣量比例数
			    int cpMoKouLiangShu = BillingUtil.getCpMoKouLiangBiLv(channelId,spid,strMo);
			    if(cpMoKouLiangShu!=0){
			    	cpKouLiang = cpMoKouLiangShu;
			    	int cpauoid = BillingUtil.getCpAutoId(channelId, spid, strMo);//查询cp对应的autoid，和下面进行区分，以免计数不对
					ischeck = BillingUtil.getCheckRecv(cpauoid,cpKouLiang);//获取是否要扣量
			    }else{
			    	cpKouLiang = cpKouLiangShu;
					ischeck = BillingUtil.getCheckRecv(channelId,cpKouLiang);//获取是否要扣量，这里直接用渠道号
			    }
			    //---------------
			    
			    StringBuilder sb = new StringBuilder();
			    sb.append("'");
			    sb.append(spid);
			    sb.append("',");
			    sb.append("'");
			    sb.append(userPhone);
			    sb.append("',");
			    sb.append("'");
			    sb.append(spNo);
			    sb.append("',");
			    sb.append("");
			    sb.append("getDate()");
			    sb.append(",");
			    sb.append("'");
			    sb.append("msgid");
			    sb.append("',");
			    sb.append("'");
			    sb.append("mid");
			    sb.append("',");
			    sb.append("'");
			    sb.append("1");//cardtype
			    sb.append("',");
			    sb.append("'");
			    sb.append(channelId);//channel
			    sb.append("',");
			    sb.append("'");
			    sb.append("101");//prdid
			    sb.append("',");
			    sb.append("'");
			    sb.append("010");//telid
			    sb.append("',");
			    sb.append("'");
			    sb.append(linkId);//linkid
			    sb.append("',");
			    sb.append("'");
			    sb.append(mo);//上行内容
			    sb.append("'");
			    sb.append(",");
			    sb.append(provinceId);
			    sb.append(",");
			    sb.append(ischeck);
			    
			    SqlUtil.executeUpdate("insert into "+BillingUtil.getRecvDay()+"(areaid,org,dest,tm,msgid,mid,cardtype,channel,prdid,teleid,linkid,body,province,ischeck) values("+sb.toString()+")",DbOperation.DB_STAT);
			    System.out.println("a8接口-上行是否扣量："+ischeck+",cp:"+channelId+",spNo:"+spNo+",mo:"+mo+",渠道扣量比例："+cpKouLiangShu+",指令扣量比例："+cpMoKouLiangShu+",最终比例扣量："+cpKouLiang);
			    if(ischeck == 0) {
			    	System.out.println("a8-上行同步:"+SqlUtil.getStringResult("select cpname from adminuser where id = "+ channelId, 5));
			    	String http = BillingUtil.getMoUrlByChannel(channelId);
			    	if(http!=null&&http.length()>0) {
			    		StringBuilder moUrl = new StringBuilder(http);
			    		if(moUrl.indexOf("?")>0){
			    			moUrl.append("&userPhone=");
			    		}else{
			    			moUrl.append("?userPhone=");
			    		}
				    	moUrl.append(userPhone);
				    	moUrl.append("&spNo=");
				    	moUrl.append(spNo);
				    	moUrl.append("&linkId=");
				    	moUrl.append(linkId);
				    	moUrl.append("&mo=");
				    	moUrl.append(URLEncoder.encode(mo,"UTF-8"));
				    	moUrl.append("&date=");
				    	moUrl.append(URLEncoder.encode(date,"UTF-8"));
					    System.out.println("a8上行同步："+moUrl.toString());
					    HttpUtil.getHtmlSource(moUrl.toString(),"UTF-8");
			    	}
			    	
			    }
			}
		}
		else if(dtype.equals("mr")){
		    System.out.println("a8接口-报告同步");
		    String spnumber = webAction.getParameterString("spnumber");
		    String userPhone = webAction.getParameterString("usernumber");
		    String linkId = webAction.getParameterString("linkid");
		    String mid = webAction.getParameterString("mid");
		    String sts = webAction.getParameterString("sts");
		    String status = webAction.getParameterString("report");
		    String srvcode = webAction.getParameterString("srvcode");
		    String feetype = webAction.getParameterString("feetype");
		    String fee = webAction.getParameterString("feecode");
		    String pdid = webAction.getParameterString("pdid");
		    //String date = webAction.getParameterString("date");
		    String date = DateUtil.nowTimeTransformCharacter(DateUtil.getNow_Timestamp().toString());
		    
			if(userPhone==null||linkId==null){
				 response.getWriter().write("1");
			}else{
				 response.getWriter().write("0");
			    String org = SqlUtil.getStringResult("select org from "+BillingUtil.getSendDay()+" where linkid = '"+linkId+"'",DbOperation.DB_STAT);
			    if(org!=null&&org.length()>0) {
			    	System.out.println("该linkid已经有同步状态");
			    	return;
			    }
			    if(sts.equals("0")){
			    	status="DELIVRD";
			    }
			    
			    String spNo = SqlUtil.getStringResult("select dest from "+BillingUtil.getRecvDay()+" where linkid = '"+linkId+"'",DbOperation.DB_STAT);
			    String mo = SqlUtil.getStringResult("select body from "+BillingUtil.getRecvDay()+" where linkid = '"+linkId+"'",DbOperation.DB_STAT);

				//指令处理,返回处理后的指令
				String strMo = BillingUtil.getMo(spNo, mo);
			    //获取用户渠道号
			    int channelId = BillingUtil.getUserChannel(spNo,strMo);
			    int spid = BillingUtil.getUserSpId(spNo,strMo);
			    //获取是否要扣量
				int ischeck = BillingUtil.getCheckSend(linkId);
			    int provinceId = BillingUtil.getProvinceId(userPhone);
			    
			    StringBuilder sb = new StringBuilder();
			    sb.append("'");
			    sb.append(spid);
			    sb.append("',");
			    sb.append("1");//msgmode
			    sb.append(",");
			    sb.append("15");//fmt
			    sb.append(",");
			    sb.append("0");//futype
			    sb.append(",");
			    sb.append("2");//ftype
			    sb.append(",");
			    sb.append(fee);//feecode  
			    sb.append(",");
			    sb.append("'");
			    sb.append("");//stype
			    sb.append("',");
			    sb.append("1");//num
			    sb.append(",");
			    sb.append("'");
			    sb.append(userPhone);
			    sb.append("',");
			    sb.append("'");
			    sb.append(spNo);
			    sb.append("',");
			    sb.append("'");
			    sb.append(userPhone);
			    sb.append("',");
			    sb.append("getDate()");  //tm
			    sb.append(",");
			    sb.append("'");
			    sb.append("mid");  //mid
			    sb.append("',");
			    sb.append("'");
			    sb.append("msgid");  //msgid
			    sb.append("',");
			    sb.append("0");//result  DELIVRD
			    sb.append(",");
			    sb.append("'");
			    sb.append(status);//DELIVRD
			    sb.append("',");
			    sb.append("'");
			    sb.append(channelId);//channelId
			    sb.append("',");
			    sb.append("'");
			    sb.append(linkId);//linkid
			    sb.append("','"+mo+"'");
			    sb.append(",");
			    sb.append(provinceId);
			    sb.append(",");
			    sb.append(ischeck);
			        
			    SqlUtil.executeUpdate("insert into "+BillingUtil.getSendDay()+"(areaid,msgMode,fmt,futype,ftype,feecode,stype,num,org,dest,fee,tm,mid,msgid,result,stat,infoid,linkid,body,province,ischeck) values("+sb.toString()+")",DbOperation.DB_STAT);
				
			    System.out.println("a8接口-状态是否需要扣量："+ischeck+",linkid:"+linkId);
			    if(ischeck == 0) {
			    	System.out.println("a8-状态同步:"+SqlUtil.getStringResult("select cpname from adminuser where id = "+ channelId, 5));
			    	String http = BillingUtil.getMtUrlByChannel(channelId);
			    	if(http!=null&&http.length()>0) {
			    		StringBuilder moUrl = new StringBuilder(http);
			    		if(moUrl.indexOf("?")>0){
			    			moUrl.append("&userPhone=");
			    		}else{
			    			moUrl.append("?userPhone=");
			    		}
				    	moUrl.append(userPhone);
				    	moUrl.append("&spNo=");
				    	moUrl.append(spNo);
				    	moUrl.append("&linkId=");
				    	moUrl.append(linkId);
				    	moUrl.append("&mo=");
				    	moUrl.append(URLEncoder.encode(mo,"UTF-8"));
				    	moUrl.append("&date=");
				    	moUrl.append(URLEncoder.encode(date,"UTF-8"));
				    	moUrl.append("&fee=");
				    	moUrl.append(fee);
				    	moUrl.append("&status=");
				    	moUrl.append(status);
					    System.out.println("a8报告同步地址："+moUrl.toString());
					    HttpUtil.getHtmlSource(moUrl.toString(),"UTF-8");
			    	}
			    }
			}
		}
	}else{
		response.getWriter().write("1");
	}


%>
    